package com.perfma.xtest.tool;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: junhui.si
 * @date: 2020-10-22 15:04
 **/
public class WordUtil {
    static WordUtil wordUtil = new WordUtil();

    public static void main(String[] args) {
        try {
            Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
            configuration.setDefaultEncoding("utf-8");
            configuration.setClassForTemplateLoading(wordUtil.getClass(), "/templates/code");
            Template template = configuration.getTemplate("demo.ftl");
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("age", "11");
            dataMap.put("sex", "南");
            dataMap.put("title1", "123");
            dataMap.put("title2", "123");
            dataMap.put("title3", "123");
            dataMap.put("title4", "123");
            dataMap.put("image1", WordGenerator.getImageString(TestFileUtil.getPath() + "1.jpg"));
            dataMap.put("image2", WordGenerator.getImageString(TestFileUtil.getPath() + "2.jpg"));
            File outFile = new File(TestFileUtil.getPath() + "demo-xxx.doc");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));

            template.process(dataMap, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
